package com.hongdee.crm.role.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.crm.role.entity.TCrmCarRole;
import com.hongdee.crm.role.service.ITcrmRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by dell on 2017/5/4.
 */
@RestController
@RequestMapping("/crm/role")
public class TCRMRoleController {
    @Autowired
    private ITcrmRoleService iTcrmRoleService;


   /*
   *
   * */

    /**
     * 保存更新
     */
    @RequestMapping("/save")
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TCrmCarRole role
    ) throws Exception {
        System.out.print(role);
        TCrmCarRole group = iTcrmRoleService.saveOrUpdate(role);
        ResultEntity res = new ResultEntity();
        res.setData(group);
        res.setSuccess(true);
        res.setMsg("保存成功！");
        System.out.println(res);

        return res;

    }

    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        Map map = iTcrmRoleService.info(id);
        System.out.println("info的id"+id);

        System.out.print(map);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        System.out.println("info的res");
        return res;
    }

    @RequestMapping("/stop")
    @ResponseBody
    public ResultEntity stop(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "0") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        iTcrmRoleService.update(id, "0");
        res.setSuccess(true);
        res.setMsg("停用成功！");
        return res;
    }

    @RequestMapping("/start")
    @ResponseBody
    public ResultEntity start(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token,
                              @RequestParam(name = "id", defaultValue = "0") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        iTcrmRoleService.update(id, "1");
        res.setSuccess(true);
        res.setMsg("启用成功！");
        return res;
    }

    @RequestMapping("/page")
    @ResponseBody
    public Map newPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                       @RequestParam(name = "token", defaultValue = "") String token,
                       @RequestParam(name = "start", defaultValue = "0") String start,
                       @RequestParam(name = "limit", defaultValue = "50") String limit,
                       @RequestParam(name = "templateName", defaultValue = "") String templateName
    ) throws Exception {
        Map map = this.iTcrmRoleService.newPage(start, limit, templateName);
        map.put("success", true);
        map.put("msg", "分页查询成功！");

        System.out.println(map);
        return map;

    }

    @RequestMapping("/combo")
    @ResponseBody

    public List<Map> combo(@RequestParam(name = "uid", defaultValue = "") String uid) throws Exception {

        return this.iTcrmRoleService.combo();
    }

    /**
     * 根据角色信息 获得角色对应的模板详细信息
     * @param uid
     * @param token
     * @param roleId
     * @return
     * @throws Exception
     */
    @RequestMapping("/template")
    @ResponseBody
    public ResultEntity template(@RequestParam(name = "uid", defaultValue = "") String uid,
                                 @RequestParam(name = "token", defaultValue = "") String token,
                                 @RequestParam(name = "roleId", defaultValue = "") String roleId) throws Exception {
        if("".equals(roleId)){
            new Exception("请选择角色");
        }
        ResultEntity res=new ResultEntity();
        res.setSuccess(true);
        res.setData(iTcrmRoleService.findTemplateByRoleId(roleId));
        return res;
    }
}